package com.taomu.app.zongchouhuli.moudle.mine.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.http.res.ResBankEntity;
import com.taomu.app.zongchouhuli.http.res.ResMyBankListEntity;
import com.taomu.app.zongchouhuli.moudle.mine.entity.AccountBankEntity;
import com.taomu.app.zongchouhuli.moudle.mine.entity.WalletListEntity;

import java.util.ArrayList;
import java.util.List;

public class AccountBankAdp extends RecyclerView.Adapter<AccountBankAdp.AccountBankHolder>{

    private List<ResMyBankListEntity.DataBean> mData = new ArrayList<>();

    private Context mContext;

    private LayoutInflater inflater;

    public AccountBankAdp(Context mContext){
        this.mData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
    }

    public void setData(List<ResMyBankListEntity.DataBean> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void addData(List<ResMyBankListEntity.DataBean> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public AccountBankHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_account,parent,false);
        return new AccountBankHolder(view);
    }

    @Override
    public void onBindViewHolder(AccountBankHolder holder, int position) {
        final ResMyBankListEntity.DataBean entity = mData.get(position);
        holder.update(entity);
        holder.mChangedTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onIitemChanged(entity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class AccountBankHolder extends RecyclerView.ViewHolder{

        private TextView mBankNumTv;

        private TextView mChangedTv;

        public AccountBankHolder(View itemView) {
            super(itemView);
            mBankNumTv = itemView.findViewById(R.id.item_account_banknum_tv);
            mChangedTv = itemView.findViewById(R.id.item_account_changed_tv);
        }

        public void update(ResMyBankListEntity.DataBean entity){
            mBankNumTv.setText(entity.getCard_num().substring(0,4)+" **** **** **** "+entity.getCard_num().substring(entity.getCard_num().length()-4,entity.getCard_num().length()));
        }
    }

    private AccountBankAdpListener listener;

    public void setAccountBankAdpListener(AccountBankAdpListener listener){
        this.listener = listener;
    }

    public interface AccountBankAdpListener{
        void onIitemChanged(ResMyBankListEntity.DataBean entity);
    }

}
